CLASS zcl_moon_i_api001m DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_rap_query_provider.
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES:
      tt_main   TYPE STANDARD TABLE OF ymoont_api001m,
      tt_detail TYPE STANDARD TABLE OF ymoont_api001d.

    METHODS call_out_api EXPORTING out_main   TYPE tt_main
                                   out_detail TYPE tt_detail
                                   search_str TYPE string.

    METHODS get_data_from_request
      IMPORTING io_request TYPE REF TO if_rap_query_request
      EXPORTING rt_result  TYPE tt_main
      RAISING   cx_rap_query_filter_no_range.
ENDCLASS.



CLASS ZCL_MOON_I_API001M IMPLEMENTATION.


  METHOD call_out_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API001'
        i_service      = 'YMOON_OS_API001_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      "out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=y220j168m038w239m000t080n2&cargMtNo=' && search_str.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    "out->write( lv_xstring ).

*    DATA:BEGIN OF ls_data,
*           cntysgn   TYPE c  LENGTH 2,
*           mtryutnm  TYPE c  LENGTH 30,
*           fxrt      TYPE c  LENGTH 13,
*           currsgn   TYPE c  LENGTH 3,
*           aplybgndt TYPE c  LENGTH 8,
*           imextp    TYPE c  LENGTH 1,
*         END OF ls_data.
*    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:lt_data TYPE tt_main,
         ls_data TYPE LINE OF tt_main.
    DATA:lt_data_detail TYPE tt_detail,
         ls_data_detail TYPE LINE OF tt_detail.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'cargCsclPrgsInfoQryVo'.
    DATA:gv_cond2 TYPE c LENGTH 50 VALUE 'cargCsclPrgsInfoDtlQryVo'.

    DATA:gv_field TYPE c LENGTH 200.
    DATA:gv_field2 TYPE c LENGTH 200.

    FIELD-SYMBOLS <fs> TYPE any.
    FIELD-SYMBOLS <fs2> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.
          gv_field2 = 'ls_data_detail-' && lv_open_node->qname-name.

          IF lv_open_node->qname-name = 'prcsDttm'.
            gv_field2 = 'ls_data_detail-' && lv_open_node->qname-name.
          ENDIF.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.

            ENDIF.
          ENDIF.


          IF lv_close_node->qname-name = gv_cond2.
            APPEND ls_data_detail TO lt_data_detail.
            CLEAR ls_data_detail.
          ELSE.
            ASSIGN (gv_field2) TO <fs2>.
            IF sy-subrc = 0.
              <fs2> = lv_value.

            ENDIF.
          ENDIF.


          CLEAR lv_value.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          CLEAR lv_value.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    MOVE-CORRESPONDING lt_data TO out_main .
    MOVE-CORRESPONDING lt_data_detail TO out_detail .



  ENDMETHOD.


  METHOD get_data_from_request.
    TYPES:
      tt_r_cargmtno TYPE RANGE OF ymoont_api001m-cargmtno,
      tt_r_mblno    TYPE RANGE OF ymoont_api001m-mblno.

    DATA lt_r_key  TYPE tt_r_cargmtno.
    DATA lt_r_text TYPE tt_r_mblno.


    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).
    DATA(ld_offset) = io_request->get_paging( )->get_offset( ).
    DATA(ld_pagesize) = io_request->get_paging( )->get_page_size( ).

    LOOP AT lt_filter INTO DATA(ls_filter).
      CASE ls_filter-name.
        WHEN 'cargMtNo'.
          lt_r_key = CORRESPONDING #( ls_filter-range ).
        WHEN 'mblNo'.
          lt_r_text = CORRESPONDING #( ls_filter-range ).

      ENDCASE.
    ENDLOOP.

    SELECT * FROM ymoont_api001m
          WHERE cargmtno IN @lt_r_key
            AND mblno IN @lt_r_text
          ORDER BY cargmtno
          INTO TABLE @rt_result
          UP TO @ld_pagesize ROWS
          OFFSET @ld_offset .


  ENDMETHOD.


  METHOD if_rap_query_provider~select.
    DATA: lt_dbtab TYPE STANDARD TABLE OF ymoont_api001m,
          ls_dbtab TYPE ymoont_api001m.
    DATA: lt_dbdetail TYPE STANDARD TABLE OF ymoont_api001d,
          ls_dbdetail TYPE ymoont_api001d.

    DATA: lt_main TYPE tt_main.
    DATA: lt_main_temp TYPE tt_main.
    DATA: lt_detail TYPE tt_detail.
    DATA: lt_detail_temp TYPE tt_detail.

    DATA: lt_database TYPE STANDARD TABLE OF ymoont_api001m.

    DATA lt_output TYPE STANDARD TABLE OF ymoon_i_api001m.
    DATA ls_output TYPE ymoon_i_api001m.
    DATA(lo_system_uuid) = NEW cl_system_uuid( ).


    DATA(lv_search) = io_request->get_search_expression( ).
    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).


    CLEAR: lt_main,lt_detail.
    IF lt_filter IS NOT INITIAL.
      LOOP AT lt_filter INTO DATA(ls_filter).
        CASE ls_filter-name.
          WHEN 'CARGMTNO'.
            LOOP AT ls_filter-range INTO DATA(ls_field).
              CLEAR:lt_main_temp,lt_detail_temp.
              call_out_api( IMPORTING out_main = lt_main_temp out_detail = lt_detail_temp search_str = ls_field-low ).
              APPEND LINES OF lt_main_temp TO lt_main.
              APPEND LINES OF lt_detail_temp TO lt_detail.
            ENDLOOP.

        ENDCASE.
      ENDLOOP.
    ELSE.
      IF lv_search IS INITIAL.
        lv_search = '21EASK14G1I60490001'.
      ENDIF.

      call_out_api( IMPORTING out_main = lt_main out_detail = lt_detail search_str = lv_search ).

    ENDIF.

    LOOP AT lt_main INTO DATA(ls_main).
      CLEAR ls_dbtab.
      MOVE-CORRESPONDING ls_main TO ls_dbtab.
      TRY.
          ls_dbtab-uuid = lo_system_uuid->if_system_uuid~create_uuid_x16( ).
        CATCH cx_uuid_error.
      ENDTRY.

      ls_dbtab-regid = sy-uname.
      ls_dbtab-regdate = sy-datum.
      APPEND ls_dbtab TO lt_dbtab.

      LOOP AT lt_detail INTO DATA(ls_detail).
        CLEAR ls_dbdetail.
        MOVE-CORRESPONDING ls_detail TO ls_dbdetail.
        TRY.
            ls_detail-uuid  = lo_system_uuid->if_system_uuid~create_uuid_x16( ).
          CATCH cx_uuid_error.
        ENDTRY.
        ls_detail-puuid = ls_dbtab-uuid .
        ls_detail-regid = sy-uname.
        ls_detail-regdate = sy-datum.

        APPEND ls_dbdetail TO lt_dbdetail.
      ENDLOOP.


    ENDLOOP.

    DELETE FROM ymoont_api001m.
    MODIFY ymoont_api001m FROM TABLE @lt_dbtab.
    DELETE FROM ymoont_api001d.
    MODIFY ymoont_api001d FROM TABLE @lt_dbdetail.


    get_data_from_request( EXPORTING io_request = io_request IMPORTING rt_result = lt_database ).
*
    CLEAR lt_output.
    LOOP AT lt_database INTO DATA(ls_line).
      CLEAR ls_output.
      MOVE-CORRESPONDING ls_line TO ls_output.

      APPEND ls_output TO lt_output.
    ENDLOOP.



    IF io_request->is_data_requested( ).
      io_response->set_data( lt_output ).
    ENDIF.

    IF io_request->is_total_numb_of_rec_requested( ).
      io_response->set_total_number_of_records( lines( lt_output ) ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.
